package A1;

import java.util.ArrayList;
import java.util.Random;

//

public class GridPrint extends GridType {

    private int count = 0;
    private boolean vRedLight = true;
    private boolean hRedLight = true;
    private int condition = 1;
    private ArrayList<Car> cArr = new ArrayList();

    public void runCycle(){

        for(; count<ProjModel.getCycle();count++){
            Random Num2 = new Random();
            double carEnter = (Num2.nextDouble()*10/10);

            if (carEnter >0 && carEnter <=ProjModel.getProbVLane()){
                cArr.add(new Car(true));} //add new car into vertical lane
            if (carEnter >0 && carEnter <=ProjModel.getProbHLane()){
                cArr.add(new Car(false));} //add new car into horizontal lane

            if (count%4 == 0){
                
                if (hRedLight == true && condition == 4){
                    hRedLight = false; //green light

                }
                else if (hRedLight == false){
                    hRedLight = true;

                }
                if (vRedLight == true && condition == 2){
                    vRedLight = false; //green light
                }
                else if (vRedLight == false)
                {
                    vRedLight = true;
                }

                condition++;
                if (condition > 4){
                    condition = 1;

                }

                assignHLight(hRedLight);//assign the status of redlight to grid
                assignVLight(vRedLight);
            }

            for(int cnt2 = 0; cnt2 < cArr.size();cnt2++)
            {
                if (cArr.get(cnt2).getVerticalCar() == true){ //for car in vertical lane
                    if (vRedLight == true){cArr.get(cnt2).stop();}
                    else if (vRedLight == false){cArr.get(cnt2).move();}
                }
                if (cArr.get(cnt2).getVerticalCar() == false){ //for car in horizontal lane
                    if (hRedLight == true){cArr.get(cnt2).stop();}
                    else if (hRedLight == false){cArr.get(cnt2).move();}
                }

                if (cArr.get(cnt2).getPos() > 45){
                    cArr.remove(cnt2);
                }
            }

            for(int x=0;x<23;x++){ //print characters inside grid array
                for(int i = 0;i<24;i++){
                    System.out.print(getGrid(x,i)+ " ");
                }
            System.out.println();
            }

        System.out.println();
        }
    }


}
